﻿using iTextSharp.text;
using iTextSharp.text.pdf;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class gentelella_master_student_Default : System.Web.UI.Page
{
    DataClassesDataContext db = new DataClassesDataContext();
    protected void Page_Load(object sender, EventArgs e)
    {
        PDF();
    }

    private void PDF()
    {
        #region 数据
        string phone = Session["phone"].ToString();
        DataClassesDataContext db = new DataClassesDataContext();
        var m = (from p in db.proc_GetStuInfobyPwd(phone)
                 select p).ToList().First();
        string wbh = m.wbh;
        var result = (from p in db.proc_GetStuInfoBywbh(wbh)
                      select p).ToList().First();
        string TabTitle = result.bkyear + "年湖北省高校普通专升本学生报名申请表";
        string StuImageUrl = Server.MapPath(@"~/StuImage/" + result.sidfy + ".jpg");
        string Sname = result.sname;
        string Sex = result.ssex;
        string Nation = result.snation;
        string Sbirth = result.sbirth;
        if (Sbirth != null)
            Sbirth = Sbirth.Substring(0, 4) + "年" + Sbirth.Substring(4, 2) + "月" + Sbirth.Substring(6, 2) + "日";
        string PoliticalStatus = result.sps;
        string Gkbmh = result.gkbmh;
        string LocalColleges = result.sdept;
        string Byzy = result.byzy;
        string Bkyx = result.bkgx;
        string Bkzy = result.proName;
        string BkzyCode = result.proNum;
        string Reward = result.getaward;
        string Specialty = result.advantage;
        string HomeAdr = result.haddress;
        string Telephone = result.sphone;
        string ContactAdr = result.lxaddress;
        string Postcode = result.postcode;
        string Sidfy = result.sidfy;
        #endregion

        #region pdf
        //字体 中文不显示解决
        //系统字体需要注意  引用iTextSharp库
        BaseFont bfchinese = BaseFont.CreateFont(@"c:\windows\fonts\MSYH.TTC,1", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
        BaseFont bftitlechinese = BaseFont.CreateFont(@"c:\windows\fonts\simhei.ttf", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
        BaseFont bfCode = BaseFont.CreateFont(@"http://localhost:33640/zsbcopy/fonts/3OF9_NEW.TTF", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
        Font ChFont = new Font(bfchinese, 12);
        Font ChFont_black = new Font(bfchinese, 20, Font.NORMAL, new BaseColor(0, 0, 0));
        Font pdfCode = new Font(bfCode, 30, Font.NORMAL, new BaseColor(0, 0, 0));
        Font TiFont_black = new Font(bftitlechinese, 16, Font.BOLD, new BaseColor(0, 0, 0));
        Font ChFont_msg = new Font(bfchinese, 12, Font.ITALIC, BaseColor.RED);

        Font font_nr = new Font(bfchinese, 10, Font.NORMAL, new BaseColor(0, 0, 0));
        Font font_nr_b = new Font(bfchinese, 14, Font.BOLD, new BaseColor(0, 0, 0));
        float padding = 5f;

        string filepath = Server.MapPath(@"~/StuPdf/Stu" + result.sidfy + ".pdf");
        Document doc = new Document(PageSize.A4);
        PdfWriter pw = PdfWriter.GetInstance(doc, new FileStream(filepath, FileMode.Create));
        doc.Open();//开始

        PdfPTable ptable = new PdfPTable(7);//7 列
        PdfPCell cell;
        float[] width = { 0.8f, 1.2f, 0.6f, 0.6f, 0.6f, 0.8f, 1.2f };
        ptable.SetWidths(width);
        #endregion
        #region 1
        cell = new PdfPCell(new Phrase("网报号：", ChFont_black));
        cell.Colspan = 4;
        cell.Border = 0;
        cell.HorizontalAlignment = Element.ALIGN_RIGHT;
        ptable.AddCell(cell);
        cell = new PdfPCell(new Phrase("*" + result.wbh + "*", pdfCode));
        cell.Colspan = 3;
        cell.Border = 0;
        cell.HorizontalAlignment = Element.ALIGN_LEFT;
        ptable.AddCell(cell);
        #endregion
        #region 2
        cell = new PdfPCell(new Phrase("\n", ChFont_black));
        cell.Colspan = 4;
        cell.Border = 0;
        ptable.AddCell(cell);
        cell = new PdfPCell(new Phrase(result.wbh + "\n", ChFont_black));
        cell.Colspan = 3;
        cell.Border = 0;
        cell.HorizontalAlignment = Element.ALIGN_CENTER;
        ptable.AddCell(cell);
        #endregion
        #region 3
        cell = new PdfPCell(new Phrase("\n" + TabTitle + "\n\n", TiFont_black));
        cell.Colspan = 7;
        cell.Border = 0;
        cell.HorizontalAlignment = Element.ALIGN_CENTER;
        ptable.AddCell(cell);
        #endregion
        doc.Add(ptable);//第一个结束
        ptable = new PdfPTable(7);//第二个开始
        ptable.SetWidths(width);
        #region 1
        //1
        cell = new PdfPCell(new Phrase("\n姓  名\n\n", font_nr));
        cell.Colspan = 1;
        cell.Padding = 3;
        cell.HorizontalAlignment = Element.ALIGN_CENTER;
        ptable.AddCell(cell);
        //2
        cell = new PdfPCell(new Phrase("\n" + Sname, font_nr));
        cell.Colspan = 1;
        cell.Padding = 3;
        cell.HorizontalAlignment = Element.ALIGN_LEFT;
        ptable.AddCell(cell);
        //3
        cell = new PdfPCell(new Phrase("\n性 别", font_nr));
        cell.Colspan = 1;
        cell.Padding = 3;
        cell.HorizontalAlignment = Element.ALIGN_CENTER;
        ptable.AddCell(cell);
        //4
        cell = new PdfPCell(new Phrase("\n" + Sex, font_nr));
        cell.Colspan = 1;
        cell.Padding = 3;
        cell.HorizontalAlignment = Element.ALIGN_LEFT;
        ptable.AddCell(cell);
        //5
        cell = new PdfPCell(new Phrase("\n民 族", font_nr));
        cell.Colspan = 1;
        cell.Padding = 3;
        cell.HorizontalAlignment = Element.ALIGN_CENTER;
        ptable.AddCell(cell);
        //6
        cell = new PdfPCell(new Phrase("\n" + Nation, font_nr));
        cell.Colspan = 1;
        cell.Padding = 3;
        cell.HorizontalAlignment = Element.ALIGN_LEFT;
        ptable.AddCell(cell);
        //7
        if (!File.Exists(StuImageUrl))
        {
            //StuImageUrl = Server.MapPath(@"~/Hbpu.jpg");
        }
        try
        {
            iTextSharp.text.Image image = iTextSharp.text.Image.GetInstance(StuImageUrl);
            cell = new PdfPCell(image, true);

        }
        catch (Exception ex)
        {
            cell = new PdfPCell(new Phrase("\n", font_nr));
            //doc.Close();
            // Response.Redirect("Error.aspx");
            //return;
        }
        cell.Colspan = 1;
        cell.Padding = 1;
        cell.Rowspan = 4;
        cell.HorizontalAlignment = Element.ALIGN_LEFT;
        ptable.AddCell(cell);
        #endregion

        #region 2
        //1
        cell = new PdfPCell(new Phrase("\n出生日期\n\n", font_nr));
        cell.Colspan = 1;
        cell.HorizontalAlignment = Element.ALIGN_CENTER;
        ptable.AddCell(cell);
        //2
        cell = new PdfPCell(new Phrase("\n" + Sbirth, font_nr));
        cell.Colspan = 1;
        cell.HorizontalAlignment = Element.ALIGN_LEFT;
        ptable.AddCell(cell);
        //3
        cell = new PdfPCell(new Phrase("\n政治面貌\n\n", font_nr));
        cell.Colspan = 2;
        cell.HorizontalAlignment = Element.ALIGN_CENTER;
        ptable.AddCell(cell);
        //4
        cell = new PdfPCell(new Phrase("\n" + PoliticalStatus, font_nr));
        cell.Colspan = 2;
        cell.HorizontalAlignment = Element.ALIGN_LEFT;
        ptable.AddCell(cell);
        #endregion
        #region 3
        //1
        cell = new PdfPCell(new Phrase("\n身份证号\n\n", font_nr));
        cell.Colspan = 1;
        cell.HorizontalAlignment = Element.ALIGN_CENTER;
        ptable.AddCell(cell);
        //2
        cell = new PdfPCell(new Phrase("\n" + Sidfy, font_nr));
        cell.Colspan = 5;
        cell.HorizontalAlignment = Element.ALIGN_LEFT;
        ptable.AddCell(cell);
        #endregion
        #region 4
        //1
        cell = new PdfPCell(new Phrase("\n高考报名号\n\n", font_nr));
        cell.Colspan = 1;
        cell.HorizontalAlignment = Element.ALIGN_CENTER;
        ptable.AddCell(cell);
        //2
        cell = new PdfPCell(new Phrase("\n" + Gkbmh, font_nr));
        cell.Colspan = 5;
        cell.HorizontalAlignment = Element.ALIGN_LEFT;
        ptable.AddCell(cell);
        #endregion
        #region 5
        //1
        cell = new PdfPCell(new Phrase("\n所在院校\n\n", font_nr));
        cell.Colspan = 1;
        cell.HorizontalAlignment = Element.ALIGN_CENTER;
        ptable.AddCell(cell);
        //2
        cell = new PdfPCell(new Phrase("\n" + LocalColleges, font_nr));
        cell.Colspan = 2;
        cell.HorizontalAlignment = Element.ALIGN_LEFT;
        ptable.AddCell(cell);
        //3
        cell = new PdfPCell(new Phrase("\n毕业专业", font_nr));
        cell.Colspan = 2;
        cell.HorizontalAlignment = Element.ALIGN_CENTER;
        ptable.AddCell(cell);
        //4
        cell = new PdfPCell(new Phrase("\n" + Byzy, font_nr));
        cell.Colspan = 2;
        cell.HorizontalAlignment = Element.ALIGN_LEFT;
        ptable.AddCell(cell);
        #endregion
        #region 6
        //1
        cell = new PdfPCell(new Phrase("\n报考高校\n\n", font_nr));
        cell.Colspan = 1;
        cell.HorizontalAlignment = Element.ALIGN_CENTER;
        ptable.AddCell(cell);
        //2
        cell = new PdfPCell(new Phrase("\n" + Bkyx, font_nr));
        cell.Colspan = 6;
        cell.HorizontalAlignment = Element.ALIGN_LEFT;
        ptable.AddCell(cell);
        #endregion
        #region 7
        //1
        cell = new PdfPCell(new Phrase("\n报考专业\n\n", font_nr));
        cell.Colspan = 1;
        cell.HorizontalAlignment = Element.ALIGN_CENTER;
        ptable.AddCell(cell);
        //2
        cell = new PdfPCell(new Phrase("\n" + Bkzy, font_nr));
        cell.Colspan = 2;
        cell.HorizontalAlignment = Element.ALIGN_LEFT;
        ptable.AddCell(cell);
        //3
        cell = new PdfPCell(new Phrase("\n报考专业代码", font_nr));
        cell.Colspan = 2;
        cell.HorizontalAlignment = Element.ALIGN_CENTER;
        ptable.AddCell(cell);
        //4
        cell = new PdfPCell(new Phrase("\n" + BkzyCode, font_nr));
        cell.Colspan = 2;
        cell.HorizontalAlignment = Element.ALIGN_LEFT;
        ptable.AddCell(cell);
        #endregion
        #region 8
        //1
        cell = new PdfPCell(new Phrase("\n\n在校期间受\n\n过何种奖励\n\n\n", font_nr));
        cell.Colspan = 1;
        cell.Rowspan = 4;
        cell.HorizontalAlignment = Element.ALIGN_CENTER;
        ptable.AddCell(cell);
        //2
        cell = new PdfPCell(new Phrase(Reward, font_nr));
        cell.Colspan = 4;
        cell.Rowspan = 4;
        cell.HorizontalAlignment = Element.ALIGN_LEFT;
        ptable.AddCell(cell);
        //3
        cell = new PdfPCell(new Phrase("\n\n有  何\n\n特  长\n\n\n", font_nr));
        cell.Colspan = 1;
        cell.Rowspan = 4;
        cell.HorizontalAlignment = Element.ALIGN_CENTER;
        ptable.AddCell(cell);
        //4
        cell = new PdfPCell(new Phrase(Specialty, font_nr));
        cell.Colspan = 1;
        cell.Rowspan = 4;
        cell.HorizontalAlignment = Element.ALIGN_LEFT;
        ptable.AddCell(cell);
        #endregion
        #region 9
        //1
        cell = new PdfPCell(new Phrase("\n家庭住址\n\n", font_nr));
        cell.Colspan = 1;
        cell.HorizontalAlignment = Element.ALIGN_CENTER;
        ptable.AddCell(cell);
        //2
        cell = new PdfPCell(new Phrase("" + HomeAdr, font_nr));
        cell.Colspan = 4; cell.VerticalAlignment = Element.ALIGN_MIDDLE;
        cell.HorizontalAlignment = Element.ALIGN_LEFT;
        ptable.AddCell(cell);
        //3
        cell = new PdfPCell(new Phrase("\n联系电话\n\n", font_nr));
        cell.Colspan = 1;
        cell.HorizontalAlignment = Element.ALIGN_CENTER;
        ptable.AddCell(cell);
        //4
        cell = new PdfPCell(new Phrase("\n" + Telephone, font_nr));
        cell.Colspan = 1;
        cell.HorizontalAlignment = Element.ALIGN_LEFT;
        ptable.AddCell(cell);
        #endregion
        #region 10
        //1
        cell = new PdfPCell(new Phrase("\n联系地址\n\n", font_nr));
        cell.Colspan = 1;
        cell.HorizontalAlignment = Element.ALIGN_CENTER;
        ptable.AddCell(cell);
        //2
        cell = new PdfPCell(new Phrase("\n" + ContactAdr, font_nr));
        cell.Colspan = 4;
        cell.HorizontalAlignment = Element.ALIGN_LEFT;
        ptable.AddCell(cell);
        //3
        cell = new PdfPCell(new Phrase("\n邮  编\n\n", font_nr));
        cell.Colspan = 1;
        cell.HorizontalAlignment = Element.ALIGN_CENTER;
        ptable.AddCell(cell);
        //4
        cell = new PdfPCell(new Phrase("\n" + Postcode, font_nr));
        cell.Colspan = 1;
        cell.HorizontalAlignment = Element.ALIGN_LEFT;
        ptable.AddCell(cell);
        #endregion
        doc.Add(ptable);//第一个结束
        ptable = new PdfPTable(3);//第二个开始
        float[] Footwidth = { 1.9f, 1.9f, 2f };
        ptable.SetWidths(Footwidth);
        #region 11
        //1
        cell = new PdfPCell(new Phrase("\n申请人须知与承诺：\n1、本人是应届普通高职/高专毕业生，能够如期毕业并获得毕业证书。\n2、本人在校期间未受到任何纪律处分。\n3、本人保证提供的信息真实\n\n\n\n申请人（签字）：\n\n\n\n\n\n\t                     年    月    日\n", font_nr));
        cell.Colspan = 1;
        cell.HorizontalAlignment = Element.ALIGN_LEFT;
        ptable.AddCell(cell);
        //2
        cell = new PdfPCell(new Phrase("\n申请者所在高校学籍管理部门审核：\n\n该生属于我校应届普通全日制在校高职（专科）学生，已取得普通专科学籍。\n\n\n\n\n负责人签字（单位盖章）：\n\n\n\n\n\n\t                     年     月     日\n\n", font_nr));
        cell.Colspan = 1;
        cell.HorizontalAlignment = Element.ALIGN_LEFT;
        ptable.AddCell(cell);
        //3
        cell = new PdfPCell(new Phrase("\n本科院校意见：\n\n\n\n\n\n\n\n\n\n负责人签字（单位盖章）：\n\n\n\n\n\n\t                     年     月     日\n\n", font_nr));
        cell.Colspan = 1;
        cell.HorizontalAlignment = Element.ALIGN_LEFT;
        ptable.AddCell(cell);
        #endregion





        //空行
        #region
        cell = new PdfPCell(new Phrase("\n", font_nr));
        cell.Colspan = 7;
        cell.Border = 0;
        cell.HorizontalAlignment = Element.ALIGN_LEFT;
        ptable.AddCell(cell);
        #endregion

        //末尾
        #region 
        cell = new PdfPCell(new Phrase("填表说明：1、本表 一式三份，申请者、申请者所在院校和举办高校各存留一份。2、所有信息在上交前必须认真核对，准确无误，如因本人填写错误导致资格审查不能通过，由申请者本人负责。3、报名手续由申请者本人办理，不得委托他人代办。填写信息不实者，一经查实，将取消其普通专升本资格。4、高考报名（考生）号由申请者所在高职院校学籍管理部门提供或填写。", font_nr));
        cell.Colspan = 7;
        cell.Border = 0;
        cell.PaddingBottom = padding;
        cell.HorizontalAlignment = Element.ALIGN_LEFT;
        ptable.AddCell(cell);
        #endregion

        doc.Add(ptable);
        doc.Close();//结束
        Response.Redirect(@"~/StuPdf/Stu" + result.sidfy + ".pdf");
    }
}